﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;
using SES.CMS.AdminCP;

namespace SES.CMS.Module
{
    public partial class ucMainMenu : System.Web.UI.UserControl
    {
        string Module = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            url = url.Substring(1, url.Length - 1);
            string url1 = url.Replace(".", "/");
            Module = url1.Substring(0, url1.IndexOf("/"));
            if (Module == "ProductCate")
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    divMenu.Visible = false;
                    divMenuProduct.Visible = true;
                    rptMenuProductMenuDataSource();
                }
            }
            else
            {
                divMenuProduct.Visible = false;
                divMenu.Visible = true;
            rptParentMenuDataSource();
             }
        }

        /// <summary>
        /// Bind menu for rptProductMenu
        /// </summary>
        /// 
        protected void rptMenuProductMenuDataSource()
        {
            rptMenuProduct.DataSource = new cmsCategoryBL().GetMenu(0);
            rptMenuProduct.DataBind();

        }
        protected void rptMenuProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)item.DataItem;

                DataTable dtChildMenu = new cmsCategoryBL().BindMenuLevel2(int.Parse(drv["CategoryID"].ToString()));

                Repeater rptChildMenu = (Repeater)item.FindControl("rptChildMenu1");
                rptChildMenu.DataSource = dtChildMenu;
                rptChildMenu.DataBind();
            }
        }
        protected void rptChildMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)item.DataItem;

                DataTable dtChildMenu = new cmsCategoryBL().GetMenu(int.Parse(drv["CategoryID"].ToString()));

                Repeater rptChildMenu = (Repeater)item.FindControl("rptChildMenu2");
                rptChildMenu.DataSource = dtChildMenu;
                rptChildMenu.DataBind();
            }
        }
        public DataTable BindMenuLevel2(int CategoryID)
        {
            return new cmsCategoryBL().GetMenu(CategoryID);
        }


        /// <summary>
        /// 
        /// </summary>
        /// 


        protected void rptParentMenuDataSource()
        {
            rptParentMenu.DataSource = new cmsCategoryBL().GetMenu(0);
            rptParentMenu.DataBind();

        }

        public string CategoryUrl(int cateID, int productType, string title)
        {
            string url = "";
            DataTable dtCate = new cmsCategoryBL().SelectOne(new cmsCategoryDO { CategoryID = cateID });
            int parentID = 0;
            if (dtCate.Rows.Count > 0)
                parentID = int.Parse(dtCate.Rows[0]["ParentID"].ToString());
            if (productType == 0)
            {
                url = "/Articles/" + cateID + "/" + FriendlyUrl(title);
            }
            else if (productType == 1)
            {
                url = "/About/" + cateID + "/" + FriendlyUrl(title);
            }
            else if (productType == 2)
            {
                if (Module == "ProductCate")
                {
                    if (parentID == 0)
                        url = "";
                    else 
                    url = "/ProductCate/" + cateID + "/" + FriendlyUrl(title);
                }
                else
                    url = "/ProductCate/" + cateID + "/" + FriendlyUrl(title);
            }
            else if (productType == 3)
            {
                url = "/Product/" + cateID + "/" + FriendlyUrl(title);
            }
            else if (productType == 4)
            {
                url = "/Contact.aspx";
            }
            return url;
        }

        public string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        protected void rptParentMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)item.DataItem;

                DataTable dtChildMenu = new cmsCategoryBL().GetMenu(int.Parse(drv["CategoryID"].ToString()));

                Repeater rptChildMenu = (Repeater)item.FindControl("rptChildMenu");
                rptChildMenu.DataSource = dtChildMenu;
                rptChildMenu.DataBind();
            }
        }

        protected DataTable GetChildData(int parentID)
        {
            return new cmsCategoryBL().GetMenu(parentID);
        }
    }
}